//this library holds funcitons for user interaction

#include <Starflub/Starflub.h>
#include <libs/libSFUI.h>

float ceil(float f) {
	int i=0;
	while(f>0) {
		f--;
		i++;
	}
	if(f>=0.5)
		f=1;
	else
		f=0;
	return i+f;
}

void SFSysLog(char *msg) {
	monitor_write_colored("[Starflub]:", LGREY_COLOR);
	monitor_write(msg);
}

void SFKernelPanic(char *msg, char *sender) {
	monitor_clear_colored(RED_COLOR);
	monitor_write_centered("IMPORTANT");
	monitor_write_bgcolored("\r\n\r\n", RED_COLOR);
	monitor_write(sender);
	monitor_write(" threw kernel panic!");
	monitor_write("\r\nMessage:");
	monitor_write(msg);
	monitor_write_bgcolored("\r\n\r\nSuspending kernel.", RED_COLOR);
	monitor_write_bgcolored("\r\n\r\nIf this is the first time you are seeing an\r\nerror like this, try rebooting your computer.", LRED_COLOR);
	monitor_write_bgcolored("\r\nOtherwise, there maybe something wrong with\r\nyour installation of Starflub.\r\n\r\n", LRED_COLOR);
#ifdef EXPERIMENTAL
	monitor_write_bgcolored("This is an experimental build of Starflub\r\nPlease report the issue at http://starflub.googlecode.com\r\n", RED_COLOR);
	monitor_write_bgcolored("We *told* you to run it in VMWare :)", RED_COLOR);
	monitor_write("\r\n\r\nFor reference, give us this code when you submit\r\nyour error report:\r\n\r\n\t");
	int refcode = (SF_VERSION_MA*1000<<8)+(SF_VERSION_MI*100<<4)+(SF_RELEASE_SET*10<<2);
	monitor_write("SFREFCODE");
	monitor_write_hex(refcode);
	monitor_write("E");
#endif
	kb_uninstall();
}

void entercb(char *unused) {
	monitor_restore_mem();
}

void SFMsgBox(char *title, char *message) {
	monitor_dump_mem();
	monitor_clear_colored(LBLUE_COLOR);
	char *tmp = (strlen(title)>strlen(message)?title:message);
	int orig_x = 80/2-strlen(tmp)/2-2;
	int orig_y = 3;
//	monitor_draw_rect(orig_x-1,orig_y,orig_x+strlen(tmp)+2,orig_y+7,DGREY_COLOR); //shadow
	monitor_draw_rect(orig_x,orig_y,orig_x+strlen(tmp)+3,orig_y+6,WHITE_COLOR);   //window
	monitor_draw_rect(orig_x,orig_y,orig_x+strlen(tmp)+3,orig_y+1,RED_COLOR);     //bar
	move_cursor_to(XY2POS(0,orig_y));
	monitor_write_centered_bgcolored(title,RED_COLOR);
	monitor_write_centered_bgcolored("\n\n", WHITE_COLOR);
	monitor_write_centered_bgcolored(message, WHITE_COLOR);
	monitor_write_centered_bgcolored("\n\n", WHITE_COLOR);
	monitor_write_centered("[OK]");
	kb_waitForEnter(&entercb);
}

/*void SFMsgBox(char *title, char *message) {
	monitor_dump_mem();
	monitor_clear_colored(LGREY_COLOR);
	monitor_write("\r\n\r\n\r\n");
	monitor_write_centered(title);
	int ilines = ceil((float)strlen(message)/40.0f);
	int i,j;
	for(i = 0; i < ilines; i++) {
		char c[40];
		for(j=i*40; j<40*(i+1); j++)
			if(j<strlen(message))
				c[j]=message[j];
		monitor_write_centered(c);
		monitor_write("\r\n");
	}
				
//		strcpy(line, message, (i*40<strlen(message)?i*40:strlen(message)-((i-1)*40)));
//		monitor_write_centered(line);
//	monitor_draw_rect(0, 0, 10, 10, LRED_COLOR);
	kb_waitForEnter(&entercb);
}*/

